<div class="layui-hide" id='enian-nav-bar'>
 	<a data-url="">应用列表</a>
</div>

<div class="layui-btn-group enian-btn-group">
	<button id='applist_add' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe61f;</i><div class="enian-btn-group-text">添加应用</div>
  	</button>
  	<button id='search' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe615;</i><div class="enian-btn-group-text">搜索</div>
  	</button>
</div>

<div id="data"></div>

<script type="text/html" id="applist_s_tool">
  <a class="layui-btn layui-btn-xs" lay-event="applist_power">子应用</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="applist_del">删除</a>
</script>
               

<script>
layui.use(['table','jquery','enianTable'], function() {
	var enianTable = layui.enianTable;
	var table = layui.table;
	var laypage = layui.laypage;
	var $ = layui.jquery;
	var tableArg={where:[],order:[]};
	var fieldData = [{tableHead:{field:'appid', width:80, title: 'Appid', sort: true},type:'noInput',typeData:{msg:'输入数字'}},
	{tableHead:{field:'order',  title: '排序',sort: true,width:80,edit: 'text'},type:"input",typeData:{msg:'请输入数字'}},
	{tableHead:{field:'name', title: '名称',edit: 'text'},type:"input",typeData:{msg:'请输入'}},
	//{tableHead:{field:'icon', width:100, title: '图标',edit: 'text'},type:"input",typeData:{msg:'请输入'}},
	{tableHead:{field:'type', width:100, title: '类型'},type:"hide",typeData:{msg:'禁止编辑'}},
	{tableHead:{field:'note', title: '备注',edit: 'text'},type:"input"},
	{tableHead:{field:'note2', width:80, title: '备注2',edit: 'text'},type:"input"},
	{tableHead:{width:130 ,align:'center',fixed: 'right',toolbar: '#applist_s_tool'},type:false}
	]
	
	
	$('#applist_add').click(function(){
		enianAdmin.go('{:url("addApp")}')
    });
    layui.link('/static/layui_extends/enianTable/enianTable.css');
    var ets = enianTable.search(fieldData);
    $('#search').click(function(){
		ets.open();
    });
    
    //监听搜索
	ets.submit(function(d){
		getTable.renderObj().reload({
		  where:{where:d}
		  ,page: {
		    curr: 1 //重新从第 1 页开始
		  }
		});
	});
    
    //渲染表格
    var getTable = enianTable.table({
		elem:"#data"
		,url:"{:url('',['t'=>1])}"
		,data:fieldData
		,config:{
			limit:30
			,height:evar.tableHeight
			,toolbar:true
			// ,skin:'line'
			,page:true
			,autoSort: false
			,method:'post'
		}
	})
 
  	//监听单元格编辑
	getTable.on('edit', function(obj) {
		var value = obj.value //得到修改后的值
    	,data = obj.data //得到所在行所有键值
   	 	,field = obj.field; //得到字段
    	var update_data="appid="+data.appid+"&field="+field+"&value="+ value;
    	$.ajax({type:'POST',url: "{:url("applist",["t"=>2])}",data:update_data, success: function(data){
    		if(data.code==1){
    			layer.msg('修改成功');
    		}else{
    			layer.alert('修改失败，请刷新后重试');
    		}
    	}});
	});
  	
  	//监听排序
	getTable.on('sort',function(obj){
		tableArg.order= obj.field + " " + obj.type;
		table.reload(getTable.id, {
		    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
		    ,where: tableArg
		});
	})

	getTable.on('tool', function(obj) {
		var data = obj.data;
		//删除数据
		if(obj.event === 'applist_del') {
			layer.confirm('确定此条删除吗？删除后不可恢复？', function(index) {
				
					layer.close(index);
					//删除
					//return;
					var d_data = "appid="+data.appid;
					$.ajax({
							type: 'POST',
							url: "{:url("applist",["t"=>3])}",
							data: d_data,
							success: function(data) {
								if(data.code == 1) {
									layer.msg('删除成功');
									obj.del();
								} else {
									layer.alert('删除失败，请刷新后重试');
								}
							}});
					});
			}
		
			/*单击子应用*/
		if(obj.event === 'applist_power') {
			enianAdmin.go('{:url("sonApplist")}?appid='+data.appid+"&appname="+data.name)
		} 
	});

});
</script>
